package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Square;
import com.skyolder.milkbee.service.SquareService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Square.
 */
@RestController
@RequestMapping("/api")
public class SquareResource {

    private final Logger log = LoggerFactory.getLogger(SquareResource.class);
        
    @Inject
    private SquareService squareService;

    /**
     * POST  /squares : Create a new square.
     *
     * @param square the square to create
     * @return the ResponseEntity with status 201 (Created) and with body the new square, or with status 400 (Bad Request) if the square has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/squares")
    @Timed
    public ResponseEntity<Square> createSquare(@RequestBody Square square) throws URISyntaxException {
        log.debug("REST request to save Square : {}", square);
        if (square.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("square", "idexists", "A new square cannot already have an ID")).body(null);
        }
        Square result = squareService.save(square);
        return ResponseEntity.created(new URI("/api/squares/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("square", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /squares : Updates an existing square.
     *
     * @param square the square to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated square,
     * or with status 400 (Bad Request) if the square is not valid,
     * or with status 500 (Internal Server Error) if the square couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/squares")
    @Timed
    public ResponseEntity<Square> updateSquare(@RequestBody Square square) throws URISyntaxException {
        log.debug("REST request to update Square : {}", square);
        if (square.getId() == null) {
            return createSquare(square);
        }
        Square result = squareService.save(square);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("square", square.getId().toString()))
            .body(result);
    }

    /**
     * GET  /squares : get all the squares.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of squares in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/squares")
    @Timed
    public ResponseEntity<List<Square>> getAllSquares(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Squares");
        Page<Square> page = squareService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/squares");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /squares/:id : get the "id" square.
     *
     * @param id the id of the square to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the square, or with status 404 (Not Found)
     */
    @GetMapping("/squares/{id}")
    @Timed
    public ResponseEntity<Square> getSquare(@PathVariable Long id) {
        log.debug("REST request to get Square : {}", id);
        Square square = squareService.findOne(id);
        return Optional.ofNullable(square)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /squares/:id : delete the "id" square.
     *
     * @param id the id of the square to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/squares/{id}")
    @Timed
    public ResponseEntity<Void> deleteSquare(@PathVariable Long id) {
        log.debug("REST request to delete Square : {}", id);
        squareService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("square", id.toString())).build();
    }

}
